package com.ukao.cashregister.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.mcxtzhang.commonadapter.rv.OnItemClickListener;
import com.ukao.cashregister.R;
import com.ukao.cashregister.adapter.RechargeDialogAdapter;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.bean.AddressList;
import com.ukao.cashregister.bean.RechargeBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.listener.MyOnItemClickListener;
import com.ukao.cashregister.listener.MyTextWatcher;
import com.ukao.cashregister.listener.OnDialogSureClickListener;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.DecimalUtil;
import com.ukao.cashregister.widget.StateButton;
import com.ukao.cashregister.widget.StateImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import butterknife.Unbinder;
import info.hoang8f.android.segmented.SegmentedGroup;

/**
 * 地址
 * Created by ukaodev on 2017/6/2.
 */

public class RechargeDialogFragment extends BaseDialogFragment {

    Unbinder unbinder;
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.cancel)
    StateButton cancel;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.tab_one)
    RadioButton tabOne;
    @BindView(R.id.tab_two)
    RadioButton tabTwo;
    @BindView(R.id.segmented_group)
    SegmentedGroup segmentedGroup;
    @BindView(R.id.recharge_edit)
    EditText rechargeEdit;
    @BindView(R.id.phone_name)
    TextView phoneName;
    @BindView(R.id.close_btn)
    StateImageView closeBtn;
    @BindView(R.id.title)
    TextView title;
    private ArrayList<RechargeBean> mRechargeData;
    private RechargeDialogAdapter mRechargeDialogAdapter;
    private AddressList currentSelect;
    //private UserInfoBean.UserBean mVipListitem;
    private OnDialogSureClickListener mOnDialogSureClickListener;
    private RechargeBean mitem;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mRechargeData = (ArrayList<RechargeBean>) getArguments().getSerializable(ARG_PARAM1);


        }

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_recharge_fragment, container, false);
        unbinder = ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        initListener();
    }

    public void setOnSureClickListener(OnDialogSureClickListener mOnDialogSureClickListener) {
        this.mOnDialogSureClickListener = mOnDialogSureClickListener;
    }


    public void initView() {
        tabOne.setChecked(true);
        title.setText("活动方案充值");
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        mRechargeDialogAdapter = new RechargeDialogAdapter(activity, mRechargeData);
        recyclerView.setAdapter(mRechargeDialogAdapter);
      //  UserInfoBean.UserBean.InfoBeanX minfo = mVipListitem.getInfo();
      //  phoneName.setText(minfo.getPhone() + " " + minfo.getName());
    }


    @Override
    protected void initListener() {
        super.initListener();
        mRechargeDialogAdapter.setOnItemClickListener(mOnItemClickListener);
        rechargeEdit.addTextChangedListener(mTextWatcher);
    }


    private TextWatcher mTextWatcher = new MyTextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            super.afterTextChanged(s);
            String str = s.toString();
            if (!CheckUtils.isEmpty(str)) {
                Constant.calculate(str, rechargeEdit);
            }
        }
    };
    private OnItemClickListener mOnItemClickListener = new MyOnItemClickListener() {
        @Override
        public void onItemClick(ViewGroup viewGroup, View view, Object o, int i) {
            super.onItemClick(viewGroup, view, o, i);
            RechargeBean item = (RechargeBean) o;
            if (!item.isCheck()) {
                item.setCheck(!item.isCheck());
                List<RechargeBean> mData = mRechargeDialogAdapter.getDatas();
                for (int j = 0; j < mData.size(); j++) {
                    if (i != j) {
                        mData.get(j).setCheck(false);

                    }
                }

                mRechargeDialogAdapter.notifyDataSetChanged();
            }

        }
    };

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @OnClick({R.id.cancel, R.id.submit,R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                dismiss();
                break;

            case R.id.close_btn:
                dismiss();
                break;
            case R.id.submit:
                String amount = "";
                if (tabOne.isChecked()) {
                    for (int i = 0; i < mRechargeData.size(); i++) {
                        if (mRechargeData.get(i).isCheck()) {
                             mitem= mRechargeData.get(i);
                            amount = mRechargeData.get(i).getAmount() + "";

                        }
                    }
                }
                if (tabTwo.isChecked()) {
                    amount = DecimalUtil.multiply(getText(rechargeEdit));
                }

                if (!CheckUtils.isEmpty(amount)) {

                    if (mOnDialogSureClickListener != null) {
                        String   count=DecimalUtil.divide(amount,"100");
                        mitem.setCount(count);
                        mOnDialogSureClickListener.onSureItemClick(mitem );
                    }

                    dismiss();
                }


                break;


        }
    }


    @OnCheckedChanged({R.id.tab_one, R.id.tab_two})
    public void OnCheckedChanged(CompoundButton view, boolean checked) {
        if (checked) {
            switch (view.getId()) {
                case R.id.tab_one:
                    hideKeyboard(rechargeEdit);
                    recyclerView.setVisibility(View.VISIBLE);
                    rechargeEdit.setVisibility(View.GONE);

                    break;
                case R.id.tab_two:
                    recyclerView.setVisibility(View.GONE);
                    rechargeEdit.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }


}
